package com.unieagle.exam.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.unieagle.exam.R;
import com.unieagle.exam.db.DatabaseContext;
import com.unieagle.exam.db.ExamDBHelper;

public class CasesActivity extends BaseActivity{

	private List<Integer> cases;
	
	private ListView mListView;
	
	private CasesAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cases_activity_layout);
		init();
	}
	
	protected void init() {
		super.init();
		mTvTitle.setText(getIntent().getStringExtra("title"));
		mListView = (ListView)findViewById(R.id.list);
		getCases();
	}
	
	private void getCases() {
		cases = new ArrayList<Integer>();
		new GetCasesTask().execute();
	}
	
	class CasesAdapter extends BaseAdapter {
		
		private List<Integer> cases;
		
		private LayoutInflater inflator;
		
		public CasesAdapter(Context ctx, List<Integer> cases) {
			this.cases = cases;
			inflator = LayoutInflater.from(ctx);
		}

		@Override
		public int getCount() {
			return cases.size();
		}

		@Override
		public Object getItem(int arg0) {
			return cases.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			TextView mTvCase;
			if (convertView == null) {
				convertView = inflator.inflate(R.layout.subject_choose2_activity_listitem, null);
				mTvCase = (TextView) convertView.findViewById(R.id.subject);
				convertView.setTag(mTvCase);
			}else {
				mTvCase = (TextView) convertView.getTag();
			}
			final int caseId = cases.get(arg0);
			mTvCase.setText(String.format("习题%d", arg0 + 1));
			convertView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(CasesActivity.this, ReviewActivity.class);
					intent.putExtra("case_id", caseId);
					intent.putExtra("subject_id", getIntent().getIntExtra("id", -1));
					startActivity(intent);
				}
			});
			return convertView;
		}
		
	}
	
	class GetCasesTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... arg0) {
			ExamDBHelper helper = new ExamDBHelper(new DatabaseContext(CasesActivity.this));
			cases = helper.getAllCasesBySubject(getIntent().getIntExtra("id", -1));
			if(cases.size() > 0) {
				return true;
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if(result) {
				mAdapter = new CasesAdapter(CasesActivity.this, cases);
				mListView.setAdapter(mAdapter);
			}
		}
		
	}
}
